
#include "linux_def.h"
#include "core.h"

int shm_alloc(shm_t *shm)
{
	int id;
	id = shmget(IPC_PRIVATE, shm->size, (SHM_R|SHM_W|IPC_CREAT));
	
	if (id == -1){
		fprintf(stderr, "shmget() failed.\n");
		return E_FAIL;
	}

	shm->addr = shmat(id, NULL, 0);

	if (shmctl(id, IPC_RMID, NULL) == -1){
		fprintf(stderr, "shmget() failed.\n");
		return E_FAIL;
	}

	return (shm->addr == (void*)-1) ? E_FAIL : E_OK;
}

void shm_free(shm_t *shm)
{
	if (shmdt(shm->addr) == -1){
		fprintf(stderr, "shmget() failed.\n");
	}
}
